package com.breaking.second.utils;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKeys;


import java.io.IOException;
import java.security.GeneralSecurityException;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class SecurePreferencesUtil {

    // Getter and setter for encryptedSharedPrefsName
    private String encryptedSharedPrefsName;

    // Constructor to initialize the preferences name
    public SecurePreferencesUtil(String prefsName) {
        this.encryptedSharedPrefsName = prefsName;
    }

    private EncryptedSharedPreferences createEncryptedSharedPreferences(Context context) {
        try {
            String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
            return (EncryptedSharedPreferences) EncryptedSharedPreferences.create(
                    encryptedSharedPrefsName,
                    masterKeyAlias,
                    context,
                    EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                    EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
            );
        } catch (GeneralSecurityException | IOException e) {
            throw new RuntimeException("Error initializing EncryptedSharedPreferences", e);
        }
    }

    private void handleException(Exception e) {
        e.printStackTrace(); // Consider logging to a file or monitoring system
    }

    public void saveEncryptedString(Context context, String key, String value) {
        try {
            EncryptedSharedPreferences prefs = createEncryptedSharedPreferences(context);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(key, value);
            editor.apply();
        } catch (Exception e) {
            handleException(e);
        }
    }

    public String getDecryptedString(Context context, String key, String defaultValue) {
        try {
            EncryptedSharedPreferences prefs = createEncryptedSharedPreferences(context);
            return prefs.getString(key, defaultValue);
        } catch (Exception e) {
            handleException(e);
            return defaultValue;
        }
    }

    public int getDecryptedInt(Context context, String key, int defaultValue) {
        try {
            EncryptedSharedPreferences prefs = createEncryptedSharedPreferences(context);
            return prefs.getInt(key, defaultValue);
        } catch (Exception e) {
            handleException(e);
            return defaultValue;
        }
    }

    public void saveEncryptedInt(Context context, String key, int value) {
        try {
            EncryptedSharedPreferences prefs = createEncryptedSharedPreferences(context);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt(key, value);
            editor.apply();
        } catch (Exception e) {
            handleException(e);
        }
    }

    public void removeKey(Context context, String key) {
        try {
            EncryptedSharedPreferences prefs = createEncryptedSharedPreferences(context);
            SharedPreferences.Editor editor = prefs.edit();
            editor.remove(key);
            editor.apply();
        } catch (Exception e) {
            handleException(e);
        }
    }

    public void clearAll(Context context) {
        try {
            EncryptedSharedPreferences prefs = createEncryptedSharedPreferences(context);
            SharedPreferences.Editor editor = prefs.edit();
            editor.clear();
            editor.apply();
        } catch (Exception e) {
            handleException(e);
        }
    }
}
